#include "qtcall.h"

#define THIS_FILE	"APP"
#define SIP_DOMAIN	"192.168.0.4"
#define SIP_USER	"1000"
#define SIP_PASSWD	"1234"


qtcall::qtcall(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);

	init();

	connect(ui.btnCall, SIGNAL(clicked()), this, SLOT(call()));
	connect(ui.btnEnd, SIGNAL(clicked()), this, SLOT(endcall()));
}

qtcall::~qtcall()
{
	if (m_acc_id!=NULL)
	{
		delete m_acc_id;
	}
}

void qtcall::init() {
	
	pj_status_t status;
	
	// create pjsua
	status = pjsua_create();
	if (status != PJ_SUCCESS) {
		QMessageBox msg;
		msg.setText("Error in pjsua_create()");
		msg.exec();
		pjsua_destroy();
		exit(1);
	}

	m_acc_id = new pjsua_acc_id();

	m_cfg = new pjsua_config();

	pjsua_config_default(m_cfg);
	m_cfg->cb.on_incoming_call = &on_incoming_call;
	m_cfg->cb.on_call_media_state = &on_call_media_state;
	m_cfg->cb.on_call_state = &on_call_state;

	m_log_cfg = new pjsua_logging_config();

	pjsua_logging_config_default(m_log_cfg);
	m_log_cfg->console_level = 4;

	status = pjsua_init(m_cfg, m_log_cfg, NULL);
	if (status != PJ_SUCCESS) {
		QMessageBox msg;
		msg.setText("Error in pjsua_init()");
		msg.exec();
		pjsua_destroy();
		exit(1);
	}

	// Add UDP transport
	m_transport_cfg = new pjsua_transport_config();

	pjsua_transport_config_default(m_transport_cfg);

	status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, m_transport_cfg, NULL);
	if (status != PJ_SUCCESS) {
		QMessageBox msg;
		msg.setText("Error creating transport");
		msg.exec();
		pjsua_destroy();
		exit(1);
	}

	// Initialization is done, now start pjsua
	status = pjsua_start();
	if (status != PJ_SUCCESS) {
		QMessageBox msg;
		msg.setText("Error starting pjsua");
		msg.exec();
		pjsua_destroy();
		exit(1);
	}

	// Register to SIP server by creating SIP account

	m_acc_cfg = new pjsua_acc_config();
	
	pjsua_acc_config_default(m_acc_cfg);
	m_acc_cfg->id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN);
	m_acc_cfg->reg_uri = pj_str("sip:" SIP_DOMAIN);
	m_acc_cfg->cred_count = 1;

	m_acc_cfg->cred_info[0].realm = pj_str(SIP_DOMAIN);
	m_acc_cfg->cred_info[0].scheme = pj_str("digest");
	m_acc_cfg->cred_info[0].username = pj_str(SIP_USER);
	m_acc_cfg->cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
	m_acc_cfg->cred_info[0].data = pj_str(SIP_PASSWD);

	status = pjsua_acc_add(m_acc_cfg, PJ_TRUE, m_acc_id);
	if (status != PJ_SUCCESS) {
		QMessageBox msg;
		msg.setText("Error adding account");
		msg.exec();
		pjsua_destroy();
		exit(1);
	}
}

void qtcall::call()
{
	
	pj_status_t status;
	

	if (ui.txtNo->text() != QString("")) {
		/*status = pjsua_verify_url(ui.txtNo->text().toStdString().c_str());
		if (status != PJ_SUCCESS) {
			QMessageBox msg;
			msg.setText("Invalid URL!");
			msg.exec();
			pjsua_destroy();
			exit(1);
		}*/

		pj_str_t uri;
		//uri =  pj_str(ui.txtNo->text().toAscii().data()); // ui.txtNo->text().toStdString().c_str();
		
		QString q_str = ui.txtNo->text();

		std::string s_str;
		s_str = q_str.toUtf8().constData();

		char * c = new char(s_str.length() + 2);
		strcpy(c, s_str.c_str());

		uri = pj_str(c);

		//if (acc_id == NULL)
		//{
		//	QMessageBox msg;
		//	msg.setText("Error account id.");
		//	msg.exec();
		//	//init();

		//}
		//else
		//{
			pjsua_acc_id accid;
			accid = *m_acc_id;
			status = pjsua_call_make_call(accid, &uri, 0, NULL, NULL, NULL);
			if (status != PJ_SUCCESS) {
				QMessageBox msg;
				msg.setText("Error making call");
				msg.exec();
				pjsua_destroy();
				//exit(1);
			}
		//}

		//delete[] c;
		
	}

}

void qtcall::endcall()
{
	pjsua_call_hangup_all();
	
}

/* Callback called by the library upon receiving incoming call */
void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
			     pjsip_rx_data *rdata)
{
    pjsua_call_info ci;

    PJ_UNUSED_ARG(acc_id);
    PJ_UNUSED_ARG(rdata);

    pjsua_call_get_info(call_id, &ci);

    PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
			 (int)ci.remote_info.slen,
			 ci.remote_info.ptr));

	//const char * info;
	printf("Incoming call from *s",ci.remote_info.ptr);
	

    /* Automatically answer incoming calls with 200/OK */
    pjsua_call_answer(call_id, 200, NULL, NULL);
}

/* Callback called by the library when call's state has changed */
void on_call_state(pjsua_call_id call_id, pjsip_event *e)
{
    pjsua_call_info ci;

    PJ_UNUSED_ARG(e);

    pjsua_call_get_info(call_id, &ci);
    PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
			 (int)ci.state_text.slen,
			 ci.state_text.ptr));
}

/* Callback called by the library when call's media state has changed */
void on_call_media_state(pjsua_call_id call_id)
{
    pjsua_call_info ci;

    pjsua_call_get_info(call_id, &ci);

    if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
	// When media is active, connect call to sound device.
	pjsua_conf_connect(ci.conf_slot, 0);
	pjsua_conf_connect(0, ci.conf_slot);
    }
}


